package cn.kailangtu.myapplication.data;

import java.util.HashMap;
import java.util.Map;

import cn.kailangtu.serialport.CommonUtils;

public class CanData {
    private static CanData instance ;
    public static CanData getInstance()
    {
        if(instance == null)
        {
            instance = new CanData();
        }
        return instance;
    }
    Map<Integer,byte[]> container ;
    public void setNewValue(int frame,byte[] data)
    {

        if(container.containsKey(frame))
        {
            byte[] value = container.get(frame);
            if(data.length >=8)
            {
                System.arraycopy(data,0,value,0,8);
            }
            else
            {
                System.arraycopy(data,0,value,0,data.length);
            }
        }
        else{
            byte[] newData = new byte[8];
            if(data.length >=8)
            {
                System.arraycopy(data,0,newData,0,8);
            }
            else
            {
                System.arraycopy(data,0,newData,0,data.length);
            }
            container.put(frame,newData);
        }

    }

    private  CanData() {
        container = new HashMap<>();

    }


    public byte[] getData(int frame)
    {
        if(container.containsKey(frame))
        {
            return container.get(frame);
        }
        else
        {
            return new byte[8];
        }
    }



    public boolean getDataBit(int frame,int byteIndex,int bitIndex)
    {
        if(byteIndex < 0 || byteIndex >7)
        {
            return false;
        }
        if(bitIndex < 0 || bitIndex >7)
        {
            return false;
        }

        byte[] target = null;
        if(container.containsKey(frame))
        {
            target  =container.get(frame);
        }
        else
        {
            target = new byte[8];
        }
        byte current = target[byteIndex];
        return ((current >> bitIndex)& 0x1) == 1;
    }



    public int getData(int frame,int byteIndex,int count,String 数据类型)
    {
        if(byteIndex < 0 || byteIndex >7)
        {
            return 0;
        }

        int all = byteIndex+ count;
        if(all > 8)
        {
            return 0;
        }

        byte[] target = null;
        if(container.containsKey(frame))
        {
            target  =container.get(frame);
        }
        else
        {
            target = new byte[8];
        }

        byte[] result = new byte[count];
        System.arraycopy(target,byteIndex,result,0,count);
        if(数据类型.startsWith("无符号")) {
            if (count == 1) {
                return result[0]& 0xFF;
            } else if (count == 2) {
                return CommonUtils.byteArrayToInt16(result[1], result[0]);
            } else if (count == 4) {
                return CommonUtils.byteArrayToInt(new byte[]{result[3], result[2], result[1], result[0]});
            }
        }else if(数据类型.startsWith("有符号")){
            if (count == 1) {
                return result[0];
            } else if (count == 2) {
                return CommonUtils.combineBytesToInt(result[1], result[0]);
            } else if (count == 4) {
                return CommonUtils.byteArrayToInt(new byte[]{result[3], result[2], result[1], result[0]});
            }
        }

        return result[0];
    }

    public int getData(int frame,int byteIndex,int count)
    {
        if(byteIndex < 0 || byteIndex >7)
        {
            return 0;
        }

        int all = byteIndex+ count;
        if(all > 8)
        {
            return 0;
        }

        byte[] target = null;
        if(container.containsKey(frame))
        {
            target  =container.get(frame);
        }
        else
        {
            target = new byte[8];
        }

        byte[] result = new byte[count];
        System.arraycopy(target,byteIndex,result,0,count);

            if (count == 1) {
                return result[0]& 0xFF;
            } else if (count == 2) {
                return CommonUtils.byteArrayToInt16(result[1], result[0]);
            } else if (count == 4) {
                return CommonUtils.byteArrayToInt(new byte[]{result[3], result[2], result[1], result[0]});
            }

        return result[0];
    }
}
